﻿Imports System.ComponentModel
Imports System.Threading
Imports System.Web

Public Class keywords
    Dim allkeywds As String = ""
    Dim nstrall As String = ""
    Dim caijikeysc As Thread
    Dim allkeyss As New Collection
    Dim keyisworking As Boolean = False
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If CheckedListBox1.SelectedIndex < 0 Then
                MsgBox("关键词不能为空！", MsgBoxStyle.OkOnly, "提示")
                Exit Sub
            End If
            keyisworking = True
            nstrall = ""
            allkeyss.Clear()
            stkeywd = CheckedListBox1.Items(CheckedListBox1.SelectedIndex).ToString
            nstrall &= stkeywd
            caijikeysc = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf caijikey))
            caijikeysc.Name = NameOf(caijikey)
            caijikeysc.Start()   '启动线程
            Button2.Enabled = False
            Button1.Enabled = True
            Button3.Enabled = False
            Button4.Enabled = False
        Catch ex As Exception
            MsgBox("获取异常，已经停止！", MsgBoxStyle.OkOnly, "提示")
            Button1.Enabled = False
            Button2.Enabled = True
            Button3.Enabled = True
            Button4.Enabled = True
            keyisworking = False
        End Try
    End Sub
    Sub caijikey()
        Do
            Try
                Dim nmid = getmid(stcountry)
                Dim nkeyword = HttpUtility.UrlEncode(stkeywd)
                If allkeyss.Count > 0 Then
                    nkeyword = HttpUtility.UrlEncode(allkeyss(1).ToString)
                    allkeyss.Remove(1)
                End If

                Dim url = "https://completion." & stcountry & "/api/2017/suggestions?limit=11&prefix=" & nkeyword & "&suggestion-type=WIDGET&suggestion-type=KEYWORD&page-type=Gateway&alias=aps&site-variant=desktop&version=3&event=onKeyPress&wc=&last-prefix=" & nkeyword & "&avg-ks-time=264&fb=1&mid=" & nmid & "&client-info=amazon-search-ui"
                Dim html = Sy.Net.Get(url,,,,,, "*",,,, System.Text.Encoding.UTF8)
                'Console.WriteLine(html)
                Dim Obj = Jsonkeywords.Deserialize(html)

                If Obj.IsNotNull() Then
                    Dim nkeyw = Obj.suggestions
                    For i = 0 To nkeyw.Count - 1
                        Dim mkeyw = nkeyw(i).value
                        If InStr(nstrall, mkeyw) <= 0 Then
                            Console.WriteLine(mkeyw)
                            allkeyss.Add(mkeyw)
                            nstrall &= mkeyw & vbCrLf
                            Invoke(Sub()
                                       CheckedListBox1.Items.Add(mkeyw)
                                   End Sub)
                        End If
                    Next
                End If
            Catch ex As Exception
                keyisworking = False
                Invoke(Sub()
                           MsgBox("获取中断，已经停止！", MsgBoxStyle.OkOnly, "提示")
                           Button1.Enabled = False
                           Button2.Enabled = True
                       End Sub)
                Exit Sub
            End Try
        Loop
        Thread.Sleep(2000)
        Application.DoEvents()
    End Sub

    Private Sub keywords_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadlist()
    End Sub
    Sub loadlist()
        Try
            allkeywds = ""
            Dim sql As String = "select * from t_TaskList7 where gj='" & stcountry & "';"
            Dim reader As Data.Common.DbDataReader = DB2(sql)
            Do While reader.Read
                CheckedListBox2.Items.Add(reader("keyword"))
                allkeywds &= reader("keyword").ToString & vbCrLf
            Loop
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub
    Function getmid(country As String) As String
        Select Case country
            Case "Amazon.com"
                Return "ATVPDKIKX0DER"
            Case "Amazon.com.mx"
                Return "A1AM78C64UM0Y8"
            Case "Amazon.ca"
                Return "A2EUQ1WTGCTBG2"
            Case "Amazon.co.uk"
                Return "A1F83G8C2ARO7P"
            Case "Amazon.fr"
                Return "A13V1IB3VIYZZH"
            Case "Amazon.de"
                Return "A1PA6795UKMFR9"
            Case "Amazon.it"
                Return "APJ6JRA9NG5V4"
            Case "Amazon.es"
                Return "A1RKKUPIHCS9HS"
            Case "Amazon.se"
                Return "A2NODRKZP88ZB9"
            Case "Amazon.nl"
                Return "A1805IZSGTT6HS"
            Case "Amazon.pl"
                Return "A1C3SOZRARQ6R3"
            Case "Amazon.com.au"
                Return "A39IBJ37TRP1C6"
            Case "Amazon.co.jp"
                Return "A1VC38T7YXB528"
            Case "Amazon.in"
                Return "A21TJRUUN4KGV"
            Case "Amazon.ae"
                Return "A2VIGQ35RCS4UG"
            Case Else
                Return "ATVPDKIKX0DER"
        End Select
    End Function

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            If CheckedListBox1.CheckedIndices.Count > 0 Then
                For i = 0 To CheckedListBox1.CheckedIndices.Count - 1
                    If InStr(allkeywds, CheckedListBox1.Items(CheckedListBox1.CheckedIndices(i)).ToString) <= 0 Then
                        CheckedListBox2.Items.Add(CheckedListBox1.Items(CheckedListBox1.CheckedIndices(i)).ToString)
                        allkeywds &= CheckedListBox1.Items(CheckedListBox1.CheckedIndices(i)).ToString & vbCrLf
                        Dim t As New TaskList7
                        t.gj = stcountry
                        t.keyword = CheckedListBox1.Items(CheckedListBox1.CheckedIndices(i)).ToString
                        t.Add()
                    End If
                Next
                loadkwy()
            Else
                MsgBox("请先勾选要移入的关键词", MsgBoxStyle.OkOnly, "提示")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If caijikeysc.IsAlive Then
                caijikeysc.Abort()
            End If
            GC.Collect()
            GC.WaitForFullGCApproach()
            Button1.Enabled = False
            Button2.Enabled = True
            Button3.Enabled = True
            Button4.Enabled = True
            keyisworking = False
            MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        Catch ex As Exception
            Button1.Enabled = False
            Button2.Enabled = True
            Button3.Enabled = True
            Button4.Enabled = True
            keyisworking = False
            MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            For i = 0 To CheckedListBox2.CheckedIndices.Count - 1
                DB2.Execute("delete from t_TaskList7 where keyword='" & Replace(CheckedListBox2.Items(CheckedListBox2.CheckedIndices(i)).ToString, "'", "''") & "'")
            Next
            CheckedListBox2.Items.Clear()
            loadlist()
            loadkwy()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub keywords_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If keyisworking = True Then
            MsgBox("请先停止再关闭！", MsgBoxStyle.OkOnly, "提示")
            e.Cancel = True
        End If
    End Sub


    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        Try
            For i As Integer = 0 To CheckedListBox2.Items.Count - 1
                CheckedListBox2.SetItemChecked(i, CheckBox1.Checked)
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        Try
            For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i, CheckBox2.Checked)
            Next
        Catch ex As Exception

        End Try
    End Sub
End Class